package com.ningmengs789.live.ui.viewholders;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.net.response.VicinityUserResponse;
import com.ningmengs789.live.server.widget.CircleImageView;
import com.ningmengs789.live.server.widget.SelectableRoundedImageView;
import com.ningmengs789.live.ui.adapter.RecommendedAdapter;
import com.bumptech.glide.Glide;
import com.ningmengs789.live.utils.ASXUtil;


public class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    /**
     * 顶部banner
     */
    private final static int VIEW_HEADER = 0;
    /**
     * 大图
     */
    private final static int VIEW_ITEM_1 = 1;
    /**
     * 中图
     */
    private final static int VIEW_ITEM_2 = 2;
    /**
     * 小图
     */
    private final static int VIEW_ITEM_3 = 3;


    private SelectableRoundedImageView mIcon;
    private Button mStateBtn;
    private TextView mStateTv;
    private TextView mNicknameTv;
    private TextView mDescTv;
    private TextView mVoice;
    private TextView mVideo;
    private LinearLayout mStateLl;


    private SelectableRoundedImageView mIcon2;
    private TextView mNicknameTv2;
    private Button mStateBtn2;
    private TextView mStateTv2;
    private TextView mVoice2;
    private TextView mVideo2;


    private CircleImageView mIcon3;
    private TextView mNicknameTv3;
    private TextView mDescTv3;
    private TextView mLocationTv3;
    private ImageView mVIPIv3;
    private ImageView mTalentIv3;
    private Context context;

    private TextView mSsl_1, mSsl_2, mSsl_3;

    private RecommendedAdapter.MyItemClickListener mItemClickListener;

    public ItemHolder (Context context,View itemView, int viewType, RecommendedAdapter.MyItemClickListener mItemClickListener) {
        super(itemView);
        this.context = context;
        if (viewType == VIEW_HEADER) {
            //
        } else if (viewType == VIEW_ITEM_1){
            mIcon =  itemView.findViewById(R.id.recommended_item_icon);
            mStateBtn = itemView.findViewById(R.id.recommended_item_state_btn);
            mStateTv = itemView.findViewById(R.id.recommended_item_state_tv);
            mNicknameTv = itemView.findViewById(R.id.recommended_item_nickname_tv);
            mDescTv = itemView.findViewById(R.id.recommended_item_desc_tv);
            mVoice = itemView.findViewById(R.id.recommended_item_voice_tv);
            mVideo = itemView.findViewById(R.id.recommended_item_video_tv);
            mStateLl = itemView.findViewById(R.id.recommended_item_state_ll);
        } else if (viewType == VIEW_ITEM_2) {
            mIcon2 =  itemView.findViewById(R.id.recommended_item2_icon);
            mNicknameTv2 =  itemView.findViewById(R.id.recommended_item2_nickname_tv);
            mStateBtn2 = itemView.findViewById(R.id.recommended_item2_state_btn);
            mStateTv2 = itemView.findViewById(R.id.recommended_item2_state_tv);
            mVoice2 = itemView.findViewById(R.id.recommended_item2_voice_tv);
            mVideo2 = itemView.findViewById(R.id.recommended_item2_video_tv);
        } else if (viewType == VIEW_ITEM_3) {
            mIcon3 =  itemView.findViewById(R.id.recommended_item3_icon);
            mNicknameTv3 =  itemView.findViewById(R.id.recommended_item3_nickname_tv);
            mDescTv3 =  itemView.findViewById(R.id.recommended_item3_desc_tv);
            mLocationTv3 =  itemView.findViewById(R.id.recommended_item3_location_tv);
            mVIPIv3 =  itemView.findViewById(R.id.recommended_item3_vip_iv);
            mTalentIv3 =  itemView.findViewById(R.id.recommended_item3_talent_iv);
        }

        if (viewType == VIEW_ITEM_1 ||
                viewType == VIEW_ITEM_2 ||
                viewType == VIEW_ITEM_3){
            mSsl_1=  itemView.findViewById(R.id.ssl_1);
            mSsl_2=  itemView.findViewById(R.id.ssl_2);
            mSsl_3=  itemView.findViewById(R.id.ssl_3);
        }

        this.mItemClickListener = mItemClickListener;
        itemView.setOnClickListener(this);
    }

    public void bind(Context context, VicinityUserResponse.ListBean listBean, int width, int viewType) {
        if (viewType == VIEW_HEADER) {
            //
        }else if (viewType == VIEW_ITEM_1){
            mNicknameTv.setText(listBean.getUser_nickname());
            String desc = listBean.getSignature();
            if (desc.length() > 14) {
                desc = desc.substring(0, 13) + "...";
            }
            mDescTv.setText(desc);

            mVoice.setText(listBean.getSpeech_cost() + context.getString(R.string.ql_cost));
            mVideo.setText(listBean.getVideo_cost() + context.getString(R.string.ql_cost));

            mStateLl.setVisibility(View.VISIBLE);
            int tint = Color.parseColor("#0aed06");
            switch (listBean.getOnline_state()) {
                case 0:
                    tint = Color.WHITE;
                    mStateTv.setText(context.getString(R.string.offline));
                    break;
                case 1:
                    tint = Color.parseColor("#0aed06");
                    mStateTv.setText(context.getString(R.string.online));
                    break;
                case 2:
                    tint = Color.parseColor("#ff6600");
                    mStateTv.setText("活跃");
                    break;
                case 3:
                    tint = Color.parseColor("#0090ff");
                    mStateTv.setText("直播中");
                    break;
                case 4:
                    tint = Color.parseColor("#ff0000");
                    mStateTv.setText("聊天中");
                    break;
                case 5:
                    tint = Color.WHITE;
                    mStateTv.setText("勿扰");
                    break;
                default:
                    break;
            }
            mStateBtn.getBackground().setColorFilter(tint, PorterDuff.Mode.SRC_ATOP);

            ViewGroup.LayoutParams params = mIcon.getLayoutParams();
            params.width = width;
            params.height = width;
            mIcon.setLayoutParams(params);
            Glide.with(context)
                    .load(listBean.getShow_photo())
                    .into(mIcon);

        } else if (viewType == VIEW_ITEM_2) {
            mNicknameTv2.setText(listBean.getUser_nickname());

            ViewGroup.LayoutParams params = mIcon2.getLayoutParams();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = width;
            mIcon2.setLayoutParams(params);
            Glide.with(context)
                    .load(listBean.getShow_photo())
                    .into(mIcon2);

            mVoice2.setText(listBean.getSpeech_cost() + context.getString(R.string.ql_cost));
            mVideo2.setText(listBean.getVideo_cost() + context.getString(R.string.ql_cost));

            int tint = Color.parseColor("#0aed06");
            switch (listBean.getOnline_state()) {
                case 0:
                    tint = Color.WHITE;
                    mStateTv2.setText(context.getString(R.string.offline));
                    break;
                case 1:
                    tint = Color.parseColor("#0aed06");
                    mStateTv2.setText(context.getString(R.string.online));
                    break;
                case 2:
                    tint = Color.parseColor("#ff6600");
                    mStateTv2.setText("活跃");
                    break;
                case 3:
                    tint = Color.parseColor("#0090ff");
                    mStateTv2.setText("直播中");
                    break;
                case 4:
                    tint = Color.parseColor("#ff0000");
                    mStateTv2.setText("聊天中");
                    break;
                case 5:
                    tint = Color.WHITE;
                    mStateTv2.setText("勿扰");
                    break;
                default:
                    break;
            }
            mStateBtn2.getBackground().setColorFilter(tint, PorterDuff.Mode.SRC_ATOP);

        } else if (viewType == VIEW_ITEM_3) {
            mNicknameTv3.setText(listBean.getUser_nickname());
            String desc = listBean.getSignature();
            if (desc.length() > 15) {
                desc = desc.substring(0, 14) + "...";
            }
            mDescTv3.setText(desc);

            String state = "";
            switch (listBean.getOnline_state()) {
                case 0:
                    state = context.getString(R.string.offline);
                    break;
                case 1:
                    state = context.getString(R.string.online);
                    break;
                case 2:
                    state = "活跃";
                    break;
                case 3:
                    state = "直播中";
                    break;
                case 4:
                    state = "聊天中";
                    break;
                case 5:
                    state = "勿扰";
                    break;
                default:
                    break;
            }
            mLocationTv3.setText(listBean.getDistance() + "·" + state);

            if (listBean.getIs_vip() == 1) {
                mVIPIv3.setVisibility(View.VISIBLE);
            }else {
                mVIPIv3.setVisibility(View.GONE);
            }

            Glide.with(context)
                    .load(listBean.getShow_photo())
                    .into(mIcon3);

            if (listBean.getDaren_status() == 2){
                mTalentIv3.setVisibility(View.VISIBLE);
            }else {
                mTalentIv3.setVisibility(View.GONE);
            }
        }

        if (viewType == VIEW_ITEM_1 ||
                viewType == VIEW_ITEM_2 ||
                viewType == VIEW_ITEM_3){

            mSsl_1.setBackgroundResource(ASXUtil.getP_BG(listBean.getSex()));
            mSsl_1.setText(ASXUtil.getP_S(listBean.getSex()));
            mSsl_2.setText(ASXUtil.getP_Shu(listBean.getShuxing()));

            String city_name = listBean.getCity_name();
            if (TextUtils.isEmpty(city_name)){
                mSsl_3.setVisibility(View.GONE);
            }else {
                mSsl_3.setVisibility(View.VISIBLE);
                mSsl_3.setText(city_name);
            }
        }
    }

    @Override
    public void onClick(View v) {
        mItemClickListener.onItemClick(v, getLayoutPosition());
    }

}

